package tg1.tralhasUteis;

import java.util.Random;
 import java.io.*;
 import java.net.*;
 
 
 
 public class PeerTeste {
 
     public static void main(String[] args) {
         
	     System.out.println("comecando o cliente");    
	     
	     int i=0,  requestsComSucesso=0;
	     long millisAntigo=0, millisAtual=0;
	     int tempoTotal=0;
	     FileWriter filewriter  =null;
	     PrintWriter log=null;
	     URL url;
	      Random random = new Random();
	     try{
	     //fazendo 1 milhao de conexoes xD
		   //url = new URL("http", "shogouki", 2000,"/announce?downloaded=0&uploaded=0&left=56&event=stopped&info_hash=-AZ2500-55JJlx8P5YMa&peer_id=-AZ2500-55JJlx8P5YMa&numwant=50&compact=1");
			 log = new PrintWriter( new FileWriter(new File("logsTempoTrackerUnicoCOMDns.txt")));
		  int maxconn=200000;
		 for (i=0; i<maxconn; i++){
			millisAtual = System.currentTimeMillis();
		
				int numAleatorio = random.nextInt(200000);
				 url = new URL("http", "shogouki.tg.", 2000,"/announce?downloaded=0&uploaded=0&left=56&event=stopped&info_hash="+numAleatorio+"&peer_id="+numAleatorio+"&numwant=50&compact=1");		
			tempoTotal += millisAtual - millisAntigo;
			if (i==0 ){
				tempoTotal=0;
			}
				log.print("conexao  "+i);
				log.println(" Diferenca de tempo  "+(millisAtual - millisAntigo));
			
				//System.out.println("conexoes feitas: "+requestsComSucesso);
				//System.out.println("zerando requests:");
				requestsComSucesso=0;
	//	 url = new URL("http", "shogouki", 2000,"/announce?downloaded=0&uploaded=0&left=56&event=stopped&info_hash="+i+"&peer_id="+i+"&numwant=50&compact=1");		
		 //System.out.println(url.toString());
				millisAtual = System.currentTimeMillis();        
				millisAntigo =millisAtual ;
			 
				/*URL url = new URL("http", "localhost", 2000,"?downloaded=0&uploaded=0&event=stopped&info_hash=jaspion&peer_id=56&numwant=15");
				*/
		
				
				/*URL url = new URL("http", "10.1.1.3", 2000, 
					"/scrape?info_hash=yw%40%be%96M%ef%24%13%f4%8fG%87%3bP1%f8%d4%acs");
				*/
		//System.out.println(url.toString());
		
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("Request-Method",    "GET");
			//connection.setDoInput(true);
			//connection.setDoOutput(true);
			connection.connect();
			
		//System.out.println("abriu a conexao com a url");    
		
	/*		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			
			StringBuffer newData = new StringBuffer(10000);
			String s = "";
			
			while (null != ((s = br.readLine()))) {
					newData.append(s);
			}
				br.close();
	*/				
		//System.out.println("leu string");    
	//			PrintWriter out = new PrintWriter(System.out, true);
				
		// out.println(new String(newData));
		
				// imprime o numero do resultado
				int code =connection.getResponseCode();
				//System.out.println("codigo de status:  "+code);    
				if (code == 200){
					requestsComSucesso++;
				}
				connection.disconnect();
				/*out.println(  "Resultado: "
					+ code
					+ "/"
					+ connection.getResponseMessage());
				*/
	//	System.out.println("terminou tudo");    
			}
			
	
	     }catch (Exception e){
		e.printStackTrace();
	     }
	     finally{
		     System.out.println("conexoes tentadas "+i);   
		     System.out.println("Tempototal "+tempoTotal);   
		     try{
			     if (filewriter!=null){
				filewriter.close();
			     }
			     if (log !=null){
				log.close();
			     }	
		     }catch (Exception e2){
			e2.printStackTrace();
		    }
	     }
     }
 }     
